package com.cs.framework.ui.builder.db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cs.framework.common.util.ContainerUtils;
import com.cs.framework.common.util.StringUtils;
import com.cs.framework.ui.dto.CSFieldProSetDto;
import com.cs.framework.ui.exception.UIExceptionHelper;
import com.cs.framework.ui.model.FieldInfo;
import com.cs.framework.ui.model.UIInfo;

/**
 * 构建字段
 * 
 * @author hedongzhou
 * 
 */
public class FieldBuilder extends AbstractDbUIBuilder {

	/**
	 * 构建字段
	 * 
	 * @param fieldId
	 *            字段ID
	 * @param superPanelId
	 *            父面板ID
	 * @return
	 */
	public UIInfo build(String fieldId, String superPanelId) {
		// 查询字段
		FieldInfo field = defDao.getField(fieldId, FieldInfo.class);
		if (field == null) {
			UIExceptionHelper.throwUIException("UI001", fieldId);
		}

		field.setProMap(new HashMap<String, Object>());

		// 查询字段属性
		List<CSFieldProSetDto> fieldProSetList = null;
		if (StringUtils.isBlank(superPanelId)) {
			fieldProSetList = defDao.getFieldProSetList(fieldId);
		} else {
			fieldProSetList = defDao.getFieldProSetList(fieldId, superPanelId);
		}
		if (ContainerUtils.listIsNotEmpty(fieldProSetList)) {
			Map<String, Object> proMap = field.getProMap();
			for (CSFieldProSetDto fieldProSet : fieldProSetList) {
				// 添加属性
				proMap.put(fieldProSet.getFieldPro(), fieldProSet.getValue());
			}
		}

		return field;
	}

}
